WOLF core
WOLF: A Modular Estimation Framework for Robotics
XXXXXXXXXXXXXXXXXXXXXXXXvoid(0)" frameborder="0" name="MSearchResults" id="MSearchResults">
solver_eigen.h
Go to the documentation of this file.
1// WOLF - Copyright (C) 2020-2026
2// Institut de Robòtica i Informàtica Industrial, CSIC-UPC.
3// Authors: Joan Solà Ortega (jsola@iri.upc.edu) and
4// Joan Vallvé Navarro (jvallve@iri.upc.edu)
5// All rights reserved.
6//
7// This file is part of WOLF: http://www.iri.upc.edu/wolf
8// WOLF is free software: you can redistribute it and/or modify
9// it under the terms of the GNU General Public License version 3
10// as published by the Free Software Foundation.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program. If not, see <http://www.gnu.org/licenses/>.
19#pragma once
20
21// wolf includes
23
24// Eigen includes
25#include <Eigen/SparseCore>
26#include <Eigen/SparseQR>
27#include <Eigen/SparseCholesky>
28#include <Eigen/OrderingMethods>
29
30#include <memory>
31
32namespace wolf
33{
35
58{
59 public:
63 {
64 QR,
65 CHOLESKY
66 };
67
68 protected:
69 // problem structure (column and row indexes)
70 std::map<StateBlockPtr, unsigned int> sb_2_col_;
71 std::map<FactorBasePtr, unsigned int> fac_2_row_;
72 std::map<StateBlockPtr, LocalParametrizationBasePtr> sb_2_local_param_;
73 std::set<StateBlockPtr> fixed_state_blocks_;
75 unsigned int n_rows_;
76 unsigned int n_cols_;
77
78 // linearized problem
79 Eigen::SparseMatrix<double> A_;
80 Eigen::VectorXd b_;
81 double cost_;
82
83 // linear solvers
86 Eigen::SparseQR<Eigen::SparseMatrix<double>, Eigen::NaturalOrdering<int>> qr_;
87 Eigen::SimplicialLDLT<Eigen::SparseMatrix<double>> ldlt_;
88 struct SuiteSparseSolvers;
89 std::unique_ptr<SuiteSparseSolvers> suitesparse_;
90 Eigen::SparseMatrix<double> R_;
91 Eigen::SparseMatrix<double> Rt_;
92 Eigen::PermutationMatrix<Eigen::Dynamic, Eigen::Dynamic, int> R_perm_;
93
94 // params
95 unsigned int max_num_iterations_;
100 unsigned int min_num_iterations_;
101
102 // summary of the last solve
103 unsigned int iterations_;
105 double final_cost_;
106 double total_time_;
108 bool failed_;
109 bool stopped_;
111 unsigned int successful_steps_;
112 unsigned int unsuccessful_steps_;
113 unsigned int line_search_steps_;
114 unsigned int n_linearizations_;
115 unsigned int n_linear_solves_;
121
122 // profiling
123 unsigned int n_iter_acc_;
124 unsigned int n_iter_max_;
125 unsigned int n_convergence_;
126 unsigned int n_interrupted_;
127 unsigned int n_failed_;
128 unsigned int n_no_convergence_;
129
130 public:
133 SolverEigen(const ProblemPtr& _wolf_problem, const YAML::Node& _params);
134
136
137 ~SolverEigen() override;
138
139 bool computeCovariancesDerived(const CovarianceBlocksToBeComputed _blocks) override;
140 bool computeCovariancesDerived(const std::vector<StateBlockPtr>& st_list) override;
141
142 bool converged() const override;
143 bool failed() const override;
144 bool wasStopped() const override;
145 unsigned int iterations() const override;
146 double initialCost() const override;
147 double finalCost() const override;
148 double totalTime() const override;
149
150 int numStateBlocksEstimatedDerived() const override;
151 int sizeStateBlocksEstimatedDerived() const override;
152 int numFactorsDerived() const override;
153 int sizeFactorsDerived() const override;
154
158
160 std::string getLinearSolverName() const;
161
163 const Eigen::SparseMatrix<double>& getJacobian() const;
164
167 std::string terminationType() const;
168
170 std::string briefReport() const;
171
173 std::string fullReport() const;
174
175 protected:
176 std::string solveDerived(const ReportVerbosity report_level) override;
177
178 void addFactorDerived(const FactorBasePtr& fac_ptr) override;
179 void removeFactorDerived(const FactorBasePtr& fac_ptr) override;
180 void addStateBlockDerived(const StateBlockPtr& state_ptr) override;
181 void removeStateBlockDerived(const StateBlockPtr& state_ptr) override;
182 void updateStateBlockStatusDerived(const StateBlockPtr& state_ptr) override;
183 void updateStateBlockLocalParametrizationDerived(const StateBlockPtr& state_ptr) override;
184
185 bool isFactorRegisteredDerived(const FactorBasePtr& fac_ptr) const override;
186 bool isStateBlockRegisteredDerived(const StateBlockPtr& state_ptr) const override;
187 bool isStateBlockFixedDerived(const StateBlockPtr& st) const override;
188 bool hasThisLocalParametrizationDerived(const StateBlockPtr& st,
189 const LocalParametrizationBasePtr& local_param) const override;
190 bool hasLocalParametrizationDerived(const StateBlockPtr& st) const override;
191
192 void printProfilingDerived(std::ostream& _stream) const override;
193 bool checkDerived(std::string prefix = "") const override;
194
196 void reindex();
197
200 bool linearize(bool _apply_loss);
201
203 bool factorize();
204
206 Eigen::VectorXd solveIncrement() const;
207
209 Eigen::MatrixXd solveNormalEquations(const Eigen::MatrixXd& _E) const;
210
212 bool computeCovarianceBlocks(const std::vector<std::pair<StateBlockPtr, StateBlockPtr>>& _pairs);
213
215 void applyIncrement(const Eigen::VectorXd& _dx);
216};
217
219{
220 return linear_solver_;
221}
222
223inline const Eigen::SparseMatrix<double>& SolverEigen::getJacobian() const
224{
225 return A_;
226}
227
228} // namespace wolf
Gauss-Newton solver using sparse linear solvers through Eigen (no ceres dependency)
Definition: solver_eigen.h:58
unsigned int n_rows_
Rows of the stacked jacobian.
Definition: solver_eigen.h:75
SolverEigen(const ProblemPtr &_wolf_problem, const YAML::Node &_params)
Constructor from the problem and the YAML parameters.
Definition: solver_eigen.cpp:92
void reindex()
Recomputes the column of each non-fixed state block and the row of each factor (if structure changed)
Definition: solver_eigen.cpp:365
unsigned int max_num_iterations_
Maximum number of iterations.
Definition: solver_eigen.h:95
bool hasLocalParametrizationDerived(const StateBlockPtr &st) const override
Returns whether a state block has a local parametrization in the derived solver.
Definition: solver_eigen.cpp:1030
bool converged_
Terminated by convergence.
Definition: solver_eigen.h:107
unsigned int line_search_steps_
Step halvings of the backtracking line search.
Definition: solver_eigen.h:113
std::map< FactorBasePtr, unsigned int > fac_2_row_
First row of each factor.
Definition: solver_eigen.h:71
double linear_solver_time_
Time spent in linear solves.
Definition: solver_eigen.h:120
std::string getLinearSolverName() const
Linear solver and its implementation, e.g. "cholesky (CHOLMOD)" or "qr (Eigen SparseQR)".
Definition: solver_eigen.cpp:1269
std::string termination_message_
Termination message.
Definition: solver_eigen.h:110
std::set< StateBlockPtr > fixed_state_blocks_
Registered fixed state blocks.
Definition: solver_eigen.h:73
unsigned int n_failed_
Solves terminated by failure.
Definition: solver_eigen.h:127
double parameter_tolerance_
Convergence tolerance on the step size.
Definition: solver_eigen.h:97
bool linearize(bool _apply_loss)
Definition: solver_eigen.cpp:445
Eigen::SparseMatrix< double > A_
Stacked jacobian (weighted by the loss function if applicable)
Definition: solver_eigen.h:79
WOLF_SOLVER_CREATE(SolverEigen)
void printProfilingDerived(std::ostream &_stream) const override
Prints the profiling of the derived solver.
Definition: solver_eigen.cpp:1097
unsigned int n_no_convergence_
Solves terminated without convergence.
Definition: solver_eigen.h:128
unsigned int n_cols_
Columns of the stacked jacobian.
Definition: solver_eigen.h:76
unsigned int successful_steps_
Accepted Gauss-Newton steps.
Definition: solver_eigen.h:111
Eigen::PermutationMatrix< Eigen::Dynamic, Eigen::Dynamic, int > R_perm_
Column permutation of the QR.
Definition: solver_eigen.h:92
const Eigen::SparseMatrix< double > & getJacobian() const
Stacked jacobian of the last linearization (with loss function weights)
Definition: solver_eigen.h:223
double initialCost() const override
Returns the initial cost of the last solve.
Definition: solver_eigen.cpp:1082
unsigned int min_num_iterations_
Iterations during which the solver cannot be interrupted.
Definition: solver_eigen.h:100
bool stopped_
Terminated by interruption.
Definition: solver_eigen.h:109
Eigen::VectorXd b_
Stacked residuals (weighted by the loss function if applicable)
Definition: solver_eigen.h:80
void addStateBlockDerived(const StateBlockPtr &state_ptr) override
Adds a state block to the derived solver.
Definition: solver_eigen.cpp:966
void applyIncrement(const Eigen::VectorXd &_dx)
Applies an increment (in the tangent space) to the non-fixed state blocks.
Definition: solver_eigen.cpp:590
bool isStateBlockFixedDerived(const StateBlockPtr &st) const override
Returns whether a state block is fixed in the derived solver.
Definition: solver_eigen.cpp:1019
bool isStateBlockRegisteredDerived(const StateBlockPtr &state_ptr) const override
Returns whether a state block is in the derived solver.
Definition: solver_eigen.cpp:1014
unsigned int n_linear_solves_
Number of linear solves.
Definition: solver_eigen.h:115
LinearSolverType getLinearSolverType() const
Returns the linear solver type.
Definition: solver_eigen.h:218
std::string briefReport() const
One line report of the last solve (as ceres::Solver::Summary::BriefReport())
Definition: solver_eigen.cpp:1187
unsigned int n_convergence_
Solves terminated by convergence.
Definition: solver_eigen.h:125
LinearSolverType linear_solver_
Linear solver type.
Definition: solver_eigen.h:84
double cost_
Cost at the current estimate.
Definition: solver_eigen.h:81
int sizeStateBlocksEstimatedDerived() const override
Returns the total size of the estimated state blocks in the derived solver.
Definition: solver_eigen.cpp:1045
Eigen::SparseMatrix< double > Rt_
Transpose of R_.
Definition: solver_eigen.h:91
bool computeCovarianceBlocks(const std::vector< std::pair< StateBlockPtr, StateBlockPtr > > &_pairs)
Computes and stores in the problem the covariance blocks of the given pairs of state blocks.
Definition: solver_eigen.cpp:868
unsigned int n_linearizations_
Number of linearizations.
Definition: solver_eigen.h:114
std::string terminationType() const
Definition: solver_eigen.cpp:1179
unsigned int iterations() const override
Returns the number of iterations of the last solve.
Definition: solver_eigen.cpp:1077
bool failed() const override
Returns whether the last solve failed.
Definition: solver_eigen.cpp:1067
void updateStateBlockStatusDerived(const StateBlockPtr &state_ptr) override
Updates the fixed/unfixed status of a state block in the derived solver.
Definition: solver_eigen.cpp:987
Eigen::SparseQR< Eigen::SparseMatrix< double >, Eigen::NaturalOrdering< int > > qr_
Columns ordered in reindex()
Definition: solver_eigen.h:86
std::unique_ptr< SuiteSparseSolvers > suitesparse_
SPQR and CHOLMOD (defined only if SuiteSparse was found)
Definition: solver_eigen.h:89
double function_tolerance_
Convergence tolerance on the cost change.
Definition: solver_eigen.h:96
double minimizer_time_
Time of the minimization stage.
Definition: solver_eigen.h:117
std::map< StateBlockPtr, LocalParametrizationBasePtr > sb_2_local_param_
Local param of each state block.
Definition: solver_eigen.h:72
unsigned int n_iter_max_
Maximum iterations of a single solve.
Definition: solver_eigen.h:124
LinearSolverType
Linear solver type.
Definition: solver_eigen.h:63
double gradient_tolerance_
Convergence tolerance on the gradient.
Definition: solver_eigen.h:98
int numStateBlocksEstimatedDerived() const override
Returns the number of estimated state blocks in the derived solver.
Definition: solver_eigen.cpp:1035
void updateStateBlockLocalParametrizationDerived(const StateBlockPtr &state_ptr) override
Updates the local parametrization of a state block in the derived solver.
Definition: solver_eigen.cpp:999
bool hasThisLocalParametrizationDerived(const StateBlockPtr &st, const LocalParametrizationBasePtr &local_param) const override
Returns whether a state block has a given local parametrization in the derived solver.
Definition: solver_eigen.cpp:1024
bool failed_
Terminated by failure.
Definition: solver_eigen.h:108
Eigen::MatrixXd solveNormalEquations(const Eigen::MatrixXd &_E) const
Solves (AᵀA)·X = E using the last factorization.
Definition: solver_eigen.cpp:574
bool converged() const override
Returns whether the last solve converged.
Definition: solver_eigen.cpp:1062
double total_time_
Total time of the solve.
Definition: solver_eigen.h:106
void removeFactorDerived(const FactorBasePtr &fac_ptr) override
Removes a factor from the derived solver.
Definition: solver_eigen.cpp:958
std::string solveDerived(const ReportVerbosity report_level) override
Solves the problem in the derived solver, returning the report.
Definition: solver_eigen.cpp:154
Eigen::SimplicialLDLT< Eigen::SparseMatrix< double > > ldlt_
Eigen's Cholesky (LDLT) of the normal equations.
Definition: solver_eigen.h:87
std::string fullReport() const
Full report of the last solve (as ceres::Solver::Summary::FullReport())
Definition: solver_eigen.cpp:1196
bool isFactorRegisteredDerived(const FactorBasePtr &fac_ptr) const override
Returns whether a factor is in the derived solver.
Definition: solver_eigen.cpp:1009
void addFactorDerived(const FactorBasePtr &fac_ptr) override
Adds a factor to the derived solver.
Definition: solver_eigen.cpp:948
bool computeCovariancesDerived(const CovarianceBlocksToBeComputed _blocks) override
Computes the covariances of the specified blocks in the derived solver.
Definition: solver_eigen.cpp:610
bool wasStopped() const override
Returns whether the last solve was stopped (interrupted by a problem change)
Definition: solver_eigen.cpp:1072
bool checkDerived(std::string prefix="") const override
Checks the consistency of the derived solver.
Definition: solver_eigen.cpp:1110
double postprocessor_time_
Time of the postprocessing stage.
Definition: solver_eigen.h:118
double linearization_time_
Time spent linearizing.
Definition: solver_eigen.h:119
double finalCost() const override
Returns the final cost of the last solve.
Definition: solver_eigen.cpp:1087
bool interrupt_on_problem_change_
Interrupt the solver if the problem changed (as SolverCeres)
Definition: solver_eigen.h:99
double totalTime() const override
Returns the total time of the last solve.
Definition: solver_eigen.cpp:1092
double initial_cost_
Cost before the solve.
Definition: solver_eigen.h:104
unsigned int unsuccessful_steps_
Rejected Gauss-Newton steps.
Definition: solver_eigen.h:112
int sizeFactorsDerived() const override
Returns the total residual size of the factors in the derived solver.
Definition: solver_eigen.cpp:1053
unsigned int iterations_
Number of iterations.
Definition: solver_eigen.h:103
double preprocessor_time_
Time of the preprocessing stage.
Definition: solver_eigen.h:116
Eigen::VectorXd solveIncrement() const
Solves A_·dx = -b_ using the last factorization.
Definition: solver_eigen.cpp:557
std::map< StateBlockPtr, unsigned int > sb_2_col_
First column of each non-fixed state block.
Definition: solver_eigen.h:70
bool factorize()
Factorizes A_ with the selected linear solver. Returns false if it failed or A_ is rank deficient.
Definition: solver_eigen.cpp:523
double final_cost_
Cost after the solve.
Definition: solver_eigen.h:105
unsigned int n_iter_acc_
Accumulated iterations of all solves.
Definition: solver_eigen.h:123
bool analysis_dirty_
The structure changed: symbolic analysis of the factorization needed.
Definition: solver_eigen.h:85
Eigen::SparseMatrix< double > R_
(sorted) R factor of the QR, used for covariances
Definition: solver_eigen.h:90
int numFactorsDerived() const override
Returns the number of factors in the derived solver.
Definition: solver_eigen.cpp:1040
bool structure_dirty_
The structure changed: reindexing needed.
Definition: solver_eigen.h:74
void removeStateBlockDerived(const StateBlockPtr &state_ptr) override
Removes a state block from the derived solver.
Definition: solver_eigen.cpp:976
~SolverEigen() override
Definition: solver_eigen.cpp:146
unsigned int n_interrupted_
Solves terminated by interruption.
Definition: solver_eigen.h:126
Solver manager for WOLF.
Definition: solver_manager.h:84
CovarianceBlocksToBeComputed
Enumeration of covariance blocks to be computed.
Definition: solver_manager.h:89
ReportVerbosity
Enumeration for the verbosity of the solver report.
Definition: solver_manager.h:102
Definition: capture_base.h:23
WOLF_PTR_TYPEDEFS(CaptureLandmarksExternal)
Definition: solver_eigen.cpp:60